package com.mbengue.corp.artisan.modele;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.NamedNativeQueries;
import javax.persistence.NamedNativeQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;


/**
 * Une feuille est un devis lors de sa creation,<br>
 * et devient une facture une fois validee.
 * @author Moustapha MBENGUE
 *
 */
@Entity
@Table(name = "feuille")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@DiscriminatorValue("Feuille")
@NamedNativeQueries(value={
    @NamedNativeQuery(name = "toutesLesFeuilles", query = "select * from feuille", resultClass = Feuille.class),
    @NamedNativeQuery(name = "toutesLesFeuillesD1Client", query = "select * from feuille where client=?", resultClass = Feuille.class),
    @NamedNativeQuery(name = "trouverFeuilleParNumero", query = "select * from feuille where type = ? and numero like ?", resultClass = Feuille.class) })
public class Feuille implements Serializable {
  private static final long serialVersionUID = -2086351390454729564L;

  @Id
  @Column(name = "numero", length = 5)
  private String numero;
  @Temporal(TemporalType.TIMESTAMP)
	@Column(name="date", nullable=false)
  private Date date;
	@OneToOne
  @JoinColumn(name = "client", referencedColumnName = "clientId", nullable = false)
	private Client client;
  @Column(name = "artisan", nullable = false, length = 100)
	private String artisan;
  @Column(name = "montant", nullable = false)
  private double montant;
  @Column(name = "type", nullable = false, insertable = false, updatable = false)
	private String type;

  public Feuille() {
    this.montant = 0.0;
	}

  public String getNumero() {
		return numero;
	}

  public void setNumero(String numero) {
		this.numero = numero;
	}

  public Date getDate() {
		return date;
	}

  public void setDate(Date date) {
		this.date = date;
	}

	public Client getClient() {
		return client;
	}

	public void setClient(Client client) {
		this.client = client;
	}

	public String getArtisan() {
		return artisan;
	}

	public void setArtisan(String artisan) {
		this.artisan = artisan;
	}

	public double getMontant() {
		return montant;
	}

	public void setMontant(double montant) {
		this.montant = montant;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

  public void majMontant(Article article, boolean ajoutOuSuppression) {
    double prixTTC = article.getPrix() * (1 + article.getTva());
    if (ajoutOuSuppression) {
      this.montant += prixTTC;
    } else {
      if (this.montant > 0 && this.montant >= prixTTC) {
        this.montant -= prixTTC;
      }
    }
  }

  @Override
  public String toString() {
    String feuString = numero + " pour le client: " + client + "; date: " + date + " fait par: "
        + artisan + " pour un montant de: " + montantArrondi();
    return feuString;
  }

  public Object[] toArray() {
    Object[] table = { numero, artisan, montantArrondi(), date, client.getNom() + " " + client.getPrenom() };
    return table;
  }

  private String montantArrondi() {
    DecimalFormat twoDForm = new DecimalFormat("####.##");
    return twoDForm.format(this.montant);
  }
}
